package kyssion.leetcode.num101_150;

import kyssion.leetcode.util.TreeNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class code107_二叉树层次遍历 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> listList  = new ArrayList<>();
        ans(root,0,listList);
        Collections.reverse(listList);
        return listList;
    }

    private void ans(TreeNode root, int index, List<List<Integer>> listList) {
        if(root==null){
            return;
        }
        if(listList.size()<=index){
            List<Integer> list = new ArrayList<>();
            list.add(root.val);
            listList.add(list);
        }else{
            listList.get(index).add(root.val);
        }
        ans(root.left,index+1,listList);
        ans(root.right,index+1,listList);
        return;
    }
}
